package com.x25175.file.util;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 徐佳辉
 * @date 2020/11/24 13:57
 */
@Slf4j
public class ExcelUtil {
    /**
     * excel 导入
     *
     * @param headerRows  表头行
     * @param needVerfiy  是否检验excel内容
     * @param pojoClass   pojo类型
     * @param <T>
     * @return
     */
    public static <T> ExcelImportResult<T> importExcel(MultipartFile file, Integer headerRows, boolean needVerfiy, Class<T> pojoClass) throws IOException {
        if (file == null) {
            log.info("文件为空");
            return null;
        }
        ImportParams params = new ImportParams();
        params.setHeadRows(headerRows); //头行忽略的行数
        params.setNeedVerify(needVerfiy); //是否开启校验
        try {
            return ExcelImportUtil.importExcelMore(file.getInputStream(), pojoClass, params);
        } catch (Exception e) {
            log.error("importExcel IOException {} ",e.getMessage());
            throw new IOException(e.getMessage());
        }
    }

}
